package com.ctl.szmd.szmd.feature_wm.ui.fragment.pager1

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.paging.LoadState
import com.ctl.lib_common.extensions.singleClick
import com.ctl.lib_common.widget.recyclerview.MyDecoration
import com.ctl.szmd.szmd.feature_wm.R
import com.ctl.szmd.szmd.feature_wm.data.model.WmDataListModel
import com.ctl.szmd.szmd.feature_wm.databinding.FragmentWmHeadlinesBinding
import com.ctl.szmd.szmd.feature_wm.ui.adapter.footer.FooterAdapter
import com.ctl.szmd.szmd.feature_wm.ui.adapter.wm.WmHomeNewsAdapter
import com.ctl.szmd.szmd.feature_wm.ui.fragment.WmWebFragment
import com.ctl.szmd.szmd.feature_wm.ui.viewmodel.WmHeadlinesViewModel
import com.ctl.szmd.szmd.feature_wm.ui.base.BaseWmBindingFragment
import kotlinx.android.synthetic.main.fragment_wm_headlines.*
import kotlinx.android.synthetic.main.view_wm_tool_bar.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collectLatest
import org.koin.android.viewmodel.compat.ScopeCompat.viewModel
import org.koin.android.viewmodel.ext.android.viewModel


class WmHeadlinesFragment : BaseWmBindingFragment<FragmentWmHeadlinesBinding>() {

    private val viewModel: WmHeadlinesViewModel by viewModel()
    private val mAdapter by lazy { WmHomeNewsAdapter() }

    override fun getLayoutId(): Int {
        return R.layout.fragment_wm_headlines
    }

    @ExperimentalCoroutinesApi
    override fun initView() {
        binding.apply {
            vm = viewModel
            lifecycle.addObserver(viewModel)
            toolbar_title.text = "妇联头条"
            toolbar_left.visibility = View.VISIBLE
            toolbar_left.singleClick {
                findNavController().navigateUp()
            }
            recycler_view.addItemDecoration(MyDecoration())
            recycler_view.adapter = mAdapter.withLoadStateFooter(FooterAdapter { mAdapter.retry() })

            refreshLayout.setOnRefreshListener {
                mAdapter.refresh()
            }
            lifecycleScope.launchWhenCreated {
                mAdapter.loadStateFlow
                    .collectLatest { state ->
                        refresh_layout.isRefreshing = state.refresh is LoadState.Loading
                    }
            }
            mAdapter.listener = View.OnClickListener {
                if (it.tag is WmDataListModel) {
                    val data = it.tag as WmDataListModel
                    val bundle = Bundle()
                    bundle.putString(WmWebFragment.TITLE, data.title)
                    bundle.putString(WmWebFragment.URL, data.pageNetPath)
                    bundle.putString(WmWebFragment.INFO_ID, data.id)
                    bundle.putString(WmWebFragment.FROM_WHERE, "妇联头条")
                    bundle.putInt(WmWebFragment.VISIT_TYPE_NUM, data.visitTypeNum ?: 0)
                    bundle.putInt(WmWebFragment.ATTACHMENT_COUNT, data.attachmentCount ?: 0)
                    findNavController().navigate(R.id.wmWebFragment, bundle)
                }
            }

            viewModel.apply {
                _emptyPageEvent.observe(viewLifecycleOwner, Observer {
                    if (it) {
                        baseViewEmpty.visibility = View.VISIBLE
                    } else {
                        baseViewEmpty.visibility = View.GONE
                    }
                })
                getHeadlineListData.observe(viewLifecycleOwner, Observer {
                    mAdapter.submitData(lifecycle, it)
                })
            }
        }
    }

}